//
//  ClubGameShareView.swift
//  77Club
//
//  Created by Apple on 2024/11/13.
//

import UIKit

class ClubGameShareView: UIView, RaeAnimationProtocol, RaeAnimationDelegate {

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: 215)
    }
    
    func animationContainerViewStyle() -> RaeAnimationViewStyle {
        .bottom
    }
    
    private let cancelButton = c_make_ex(UIButton.self)
        .corner(8.0.Scale).bgColor(.white).text("取消")
        .font(15.font_semiBold).color("#2E3033").end()
    private var shareView = ClubMoreShareView()

    var copyShareBlock: ((_ type: Int) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = "#E8E9EB".toColor
        rae.corner(12.0.Scale, corners: [.layerMinXMinYCorner,.layerMaxXMinYCorner])
        
        subviews{
            shareView
            cancelButton
        }
        
        shareView.top(20.0.Scale).leading(16.0.Scale).trailing(16.0.Scale).height(103.0.Scale)
        cancelButton.leading(16.0.Scale).trailing(16.0.Scale).height(45.0.Scale)
        cancelButton.Top == shareView.Bottom + 9.0.Scale
        
        shareView.copyShareBlock = { type in
            self.entryAnimate.animation(dismiss: self){
                self.copyShareBlock?(type)
            }
        }
        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
    }
    
    @objc private func clickCancelButton(){
        entryAnimate.animation(dismiss: self)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ClubGameMoreView: UIView, UITableViewDelegate, UITableViewDataSource, RaeAnimationProtocol, RaeAnimationDelegate {

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: 160)
    }
    
    func animationContainerViewStyle() -> RaeAnimationViewStyle {
        .bottom
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubMoreItemCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 45.0.Scale
        tableView.layer.cornerRadius = 8.0.Scale
        tableView.layer.masksToBounds = true
        return tableView
    }()
    private let cancelButton = c_make_ex(UIButton.self)
        .corner(8.0.Scale).bgColor(.white).text("取消")
        .font(15.font_semiBold).color("#2E3033").end()
    
    var uid_des = "" {
        didSet{
            loadData()
        }
    }
    
    private var models = [ClubMoreModel]()
    
    var reportBlock: (() -> Void)?
    var delBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = "#E8E9EB".toColor
        rae.corner(12.0.Scale, corners: [.layerMinXMinYCorner,.layerMaxXMinYCorner])
        
        subviews{
            tableView
            cancelButton
        }
        
        tableView.top(20.0.Scale).leading(16.0.Scale).trailing(16.0.Scale).height(45.0.Scale)
        cancelButton.leading(16.0.Scale).trailing(16.0.Scale).height(45.0.Scale)
        cancelButton.Top == tableView.Bottom + 9.0.Scale

        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
    }
    
    private func loadData(){
        
        var title = "举报此评论"
        var img = "Home_more_report"
        if let uid = UserDefaultsManager.getString(forKey: .uid), uid == uid_des {
            title = "删除评论"
            img = "Home_more_del"
        }
        
        let delModel = ClubMoreModel(title: title, img: img, isRed: true)
        models.append(delModel)
        
        self.tableView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return models.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubMoreItemCell", for: indexPath) as! ClubMoreItemCell
        cell.model = models[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        entryAnimate.animation(dismiss: self){
            if self.models[indexPath.row].title == "删除评论"{
                self.delBlock?()
            }else{
                self.reportBlock?()
            }
        }
    }
    
    @objc private func clickCancelButton(){
        entryAnimate.animation(dismiss: self)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
